<?php


class Crawl_sina_from_phone_json_library  extends Crawl_keyword_library{

    private $_header;
    private $_userAgent;
    private $_url;
    private $_cookie;
    protected $_parameters = '';
    public  $_htmlsearch_config;

    /**
     * 定义 CURL 消息头、用户端信息、抓取地址
     * 定义 新浪网页抓取选项
     */
    function __construct(){
        parent::__construct();
        $openplatforms = $this->_ci->config->item('openplatforms');
        $this->_htmlsearch_config  = $openplatforms['openplatforms']['htmlsearch']['sina_phone'];
        $this->_null_tab    = $this->_htmlsearch_config['null'];

        $this->_header      = $this->_htmlsearch_config['header'];
        $this->_userAgent   = $this->_htmlsearch_config['user_agent'];
        $this->_url         = $this->_htmlsearch_config['url'];

        $this->write_log_stary_tag();
    }


    public function crawl($retry = FALSE) {
        $crawl_result = $this->try_crawl($retry);

        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        } else if(is_array($crawl_result) && array_key_exists('data', $crawl_result) && is_null($crawl_result['data'])) {
            return $crawl_result;
        } else {
            return $this->make_data($crawl_result);
        }
    }

    protected function make_data($result = NULL) {

        echo 'nope';

        return false;
    }

    /**
     * 尝试抓取
     * @param bool $retry 是否使用代理和重试功能
     * @return array|string 错误时返回error数组 正确时返回抓取到的字符穿
     */

    protected function try_crawl($retry = FALSE) {
        //临时数组
        $temp_array = array();
        //定义抓取参数
        $parameters = $this->make_parameters();
        if(is_array($parameters) && array_key_exists($this->_error_string, $parameters)) {
            log_message($this->_error_string, 'Crawl From Sina Wap Make CURL Parameters Error', FALSE, $this->_log_dir);
            return $parameters;
        }
        if($retry == FALSE) {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From Sina Wap Un Use Retry And Un Use Proxy With Parameters: '.json_encode($this->_parameters), FALSE, $this->_log_dir);
            return self::get_content($this->_parameters);
        } else {
            $crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip($this->_parameters, self::PRROXY_IP, $this->_crawl_from['sina_phone'], TRUE);
            if(is_array($crawl_use_proxy_ip_result)) {
                if(!array_key_exists($this->_error_string, $crawl_use_proxy_ip_result)) {
                    $temp_array = $crawl_use_proxy_ip_result;
                }

                $re_crawl_result = $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from['sina_phone'], TRUE);

                if(is_array($re_crawl_result) && !array_key_exists($this->_error_string, $re_crawl_result) && !array_key_exists($this->_null_data_array_key, $re_crawl_result)) {
                    return $this->make_crawl_return_array($temp_array, $re_crawl_result);
                } else if(count($temp_array) > 0) {
                    return $this->make_crawl_return_array($temp_array);
                }else {
                    return $re_crawl_result;
                }
            } else {
                return $crawl_use_proxy_ip_result;
            }
        }
    }

    /**
     * 抓取内容验证
     * @param $html
     * @return bool
     */
    public function match_html($html) {
    	
        if(strpos($html, $this->_htmlsearch_config['no_login'])) {
            
            //如果返回登陆页 重新组织参数 更新COOKIE
            $this->make_parameters();
            
            //$this->label_cookie($this->_cookie['id']);
            return $this->_ci->error_code_library->getError('curl', 'data', 'cookie_error', $this->_htmlsearch_config['no_login']);
        } elseif(strpos($html, $this->_htmlsearch_config['result_error'])) {
            $this->label_proxy_table($this->_proxy_ip, $this->_use_proxy);
            return $this->_ci->error_code_library->getError('curl', 'data', 'verification_code', $this->_htmlsearch_config['result_error']);
        } elseif(strpos($html, $this->_htmlsearch_config['result_success']) === FALSE) {
            return $this->_ci->error_code_library->getError('curl', 'data', 'incomplete_answers', $this->_htmlsearch_config['result_success']);
        } else {
            return TRUE;
        }

    }
    
    //2016年12月21日
    function make_parameters() {
        echo 'nope';
        return false;
    	//若传入cookie信息.则使用传入的cookie信息,用于调试cookie 是否有效使用 add by hejigui
    
    	$parameters['url'] = 'http://m.weibo.cn/container/getIndex?type=uid&containerid=107603'.trim($this->_ci->input->post('uid')).'&page='.$this->_ci->input->post('page');
    	return $this->_parameters = $parameters;
    }


}
